<template>
  <div class="login-container" @keyup.enter="handleSubmit">
    <el-form ref="loginForm" :model="loginForm" class="login-form" :rules="loginRules">
      <div class="title-container">
        <h3 class="title">康庄物流计算系统</h3>
      </div>
      <el-form-item prop="userName">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          v-model.trim="loginForm.userName"
          placeholder="用户名"
        />
      </el-form-item>

      <!-- <el-tooltip v-model="capsTooltip" content="Caps lock is On" placement="right" manual> -->
      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="密码"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>
      <el-button type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleSubmit">登录</el-button>
    </el-form>
  </div>
</template>

<script>
// import { validname } from '@/utils/validate.js'
import aesUtil from '@/utils/aesUtil'
import slider from '../components/slider/slider.vue' // 登录验证

export default {
  // name: 'Login',
  components: { slider },
  data() {
    // const validateuserName = (rule, value, callback) => {
    //   if (!value) {
    //     return callback(new Error('用户名不能为空!'))
    //   } else if (validname(value)) {
    //     callback()
    //   } else {
    //     callback(new Error('4-14位数字字母，勿以数字开头!'))
    //   }
    // }
    return {
      sliderShow: false,
      loginForm: {
        userName: '',
        password: ''
      },
      loginRules: {
        userName: [{ required: true, message: '请输入账号!', trigger: 'change' }],
        password: [{ required: true, message: '请输入密码!', trigger: 'change' }]
      },
      passwordType: 'password',
      loading: false
    }
  },
  mounted() {
    this.$store.dispatch('user/resetToken')
  },
  methods: {
    handleSubmit() {
      const account1 = ['kangzhuang', 'kangzhuang888']
      const account2 = ['kangzhuang', 'kangzhuang666']
      const { userName, password } = this.loginForm
      const isYunying = userName === account1[0] && password === account1[1]
      const isKefu = userName === account2[0] && password === account2[1]
      console.log(isYunying, isKefu)

      if (isYunying || isKefu) {
        // alert('账号密码输入有误!')
        if (isYunying) {
          localStorage.setItem('role', 'yunying')
        }
        if (isKefu) {
          localStorage.setItem('role', 'kefu')
        }
        this.$refs['loginForm'].validate(valid => { // 登录之前的校验
          if (valid) {
            // console.log(111)
            const loginForm = {
              userName: this.loginForm.userName.trim(),
              // password: aesUtil.AES_Encrypt(this.loginForm.password)
              password: this.loginForm.password
            }
            this.$store.dispatch('user/login', loginForm).then((res) => {
              this.$router.push({ path: '/' })// 登录成功之后重定向到欢迎页
            }).catch(error => {
              this.$message({
                type: 'error',
                message: error
              })
              this.sliderShow = false
            })
            // this.$router.push({ path: '/' })// 登录成功之后重定向到欢迎页
          }
        })
      } else {
        alert('账号密码输入有误!')
      }
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    }
  }
}
</script>

<style lang="scss">
$bg:#283443;
$light_gray:#fff;
// $cursor: #fff;
$cursor: #000;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  background-image: url('~@/assets/images/bgc.jpg');
  // background-size: cover;
  background-size: 100% 100%;
  background-repeat: no-repeat;
  z-index: 0;
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: #333;
      height: 47px;
      caret-color: #333;

      &:-webkit-autofill {
        // box-shadow: 0 0 0px 1000px $bg inset !important;
        box-shadow: 0 0 0px 1000px #fff inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }
  // /deep/ .el-input__inner {
  //   background-color: #fff !important;
  // }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    // background: rgba(0, 0, 0, 0.1);
    background: #fff;
    border-radius: 5px;
    color: #454545;
    z-index: 99;
  }
}
</style>

<style lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 30px;
      // color: $dark_gray;
      color: #fff;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
      word-wrap: nowrap;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }

  .thirdparty-button {
    position: absolute;
    right: 0;
    bottom: 6px;
  }

  @media only screen and (max-width: 470px) {
    .thirdparty-button {
      display: none;
    }
  }

  /deep/ .el-input__inner {
    // background-color: pink !important;
  }
}
</style>
